package com.example.myapplication.ui.activity.login

import android.text.TextUtils
import android.widget.Toast
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.kun.base.ui.AbsBaseActivity
import com.kun.base.util.ToastUtils
import kotlinx.android.synthetic.main.activity_register.*
import kotlinx.android.synthetic.main.toolbar_layout.*

/**
 *注册的activity
 *ResponseResult(errorCode=5000, errorMsg=系统错误(@FieldMap parameters can only be used with form encoding. (parameter #1)
for method IApi.register), data=null)
 * @author : Kun
 * @date : 2021/6/4
 * @time : 13:23
 */
@Route(path = ARouterPath.REGISTER)
class RegisterActivity : AbsBaseActivity() {
    private lateinit var registerViewModel: RegisterViewModel

    override fun getLayoutId(): Int {
        return R.layout.activity_register
    }

    override fun initViewModel() {
        registerViewModel = getActivityScopeViewModel(RegisterViewModel::class.java)
        registerViewModel.loginRequest.registerResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show()
                finish()
            } else {
                Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show()
            }
        })
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        tv_title.text = getString(R.string.register)

        tv_register.setOnClickListener {
            register()
        }
    }

    /**
     * 注册
     */
    private fun register() {
        val userName = et_account.text.toString()
        if (TextUtils.isEmpty(userName)) {
            ToastUtils.showShortToast("请输入用户名")
            return
        }
        val password = et_password.text.toString()
        if (TextUtils.isEmpty(password)) {
            ToastUtils.showShortToast("请输入密码")
            return
        }

        val rePassword = et_password.text.toString()
        if (TextUtils.isEmpty(rePassword)) {
            ToastUtils.showShortToast("请再次输入密码")
            return
        }
        if (password != rePassword) {
            ToastUtils.showShortToast("输入密码不一致")
            return
        }
        registerViewModel.requestRegister(userName, password, rePassword)
    }
}